package com.ag.score.apicontroller;

import com.alibaba.fastjson.JSONObject;

import com.ag.common.api.R;
import com.ag.common.api.ResultCode;
import com.ag.score.apiservice.AppService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/order")
public class OrderApiController {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderApiController.class);
    @Autowired
    AppService appService;

    @PostMapping("/record")
    @ApiOperation(value = "添加",notes = "1失败 0 成功,参数(例) {\"token\":\"123456\",\"type\":\"1\",\"score\":\"100\"   }")
    public R record(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject paramJson){
        response.setHeader("Access-Control-Allow-Origin", "*");

        try{


            boolean flag = appService.addRecord(paramJson);
            if(flag) {
                return R.data(ResultCode.SUCCESS.getCode(), paramJson.get("resultStr").toString());
            }else{
                return R.fail(1,paramJson.get("resultStr").toString());
            }


        }catch (Exception e){
            LOGGER.error("OrderApiController record error:"+e.getMessage());
            return R.fail(1, "发生异常，获取失败"+e.getMessage());
        }

    }
    @PostMapping("/recording")
    @ApiOperation(value = "未结束记录",notes = "1失败 0 成功,参数(例) {\"token\":\"123456\"   }")
    public R recordIng(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject paramJson){
        response.setHeader("Access-Control-Allow-Origin", "*");

        try{
            paramJson.put("status",0);

            boolean flag = appService.recordPage(paramJson);
            if(flag) {
                return R.data(ResultCode.SUCCESS.getCode(), paramJson.get("resultStr").toString());
            }else{
                return R.fail(1,paramJson.get("resultStr").toString());
            }


        }catch (Exception e){
            LOGGER.error("OrderApiController recordIng error:"+e.getMessage());
            return R.fail(1, "发生异常，获取失败"+e.getMessage());
        }

    }
    @PostMapping("/recordend")
    @ApiOperation(value = "结束记录",notes = "1失败 0 成功,参数(例) {\"token\":\"123456\",\"pageNo\": 0    }")
    public R recordEnd(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject paramJson){
        response.setHeader("Access-Control-Allow-Origin", "*");

        try{
            paramJson.put("status",1);

            boolean flag = appService.recordPage(paramJson);
            if(flag) {
                return R.data(ResultCode.SUCCESS.getCode(), paramJson.get("resultStr").toString());
            }else{
                return R.fail(1,paramJson.get("resultStr").toString());
            }


        }catch (Exception e){
            LOGGER.error("OrderApiController recordEnd error:"+e.getMessage());
            return R.fail(1, "发生异常，获取失败"+e.getMessage());
        }

    }
    @PostMapping("/queryparam")
    @ApiOperation(value = "参数",notes = "1失败 0 成功,参数(例) ")
    public R queryParam(HttpServletRequest request, HttpServletResponse response ){
        response.setHeader("Access-Control-Allow-Origin", "*");

        try{


            JSONObject res = appService.queryParam( );

            return R.data(ResultCode.SUCCESS.getCode(), res.toString());



        }catch (Exception e){
            LOGGER.error("OrderApiController recordEnd error:"+e.getMessage());
            return R.fail(1, "发生异常，获取失败"+e.getMessage());
        }

    }
}
